package vn.edu.saigontech.ncms.web.offering;

import java.util.Calendar;
import java.util.Date;
import java.util.Set;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateVideoScheduleAction extends AbstractValidator {
	private Long id;
	private Long offeringId;
	private String date;

	@Override
	public String check() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		if (Checker.isNull(this.offeringId)) {
			this.sendKeyError("validator.required", "class");
		}
		if (Checker.isEmpty(this.date)) {
			this.sendKeyError("validator.required", "date");
		}
		if (!Checker.isDate(this.date, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "date", "format.date");
		}
		Set<Integer> dates = offering.getDays();
		Date dateDate = DateUtil
				.fromString(this.date, this.i18n("format.date"));
		Calendar caledar = DateUtil.toCalendar(dateDate);
		int inputDate = caledar.get(Calendar.DAY_OF_WEEK);

		if(!DateUtil.isBetween(dateDate, offering.getStartDate(), offering.getEndDate())) {
			String f = DateUtil.toString(offering.getStartDate(), this.i18n("format.date"));
			String t = DateUtil.toString(offering.getEndDate(), this.i18n("format.date"));
			this.sendKeyError("validator.between", "date", f, t);
		}
		if (!dates.contains(inputDate)) {
			this.sendKeyError("validator.timeTable", "date");
		}
		return this.toSuccess();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

}
